package cn.shyouchi.wuguanjia.android.ui.event.arrange;

import android.content.Context;
import android.support.annotation.NonNull;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.shyouchi.wuguanjia.android.api.event.EventApi;
import cn.shyouchi.wuguanjia.android.model.BaseRequestResult;
import cn.shyouchi.wuguanjia.android.subscribers.ProgressSubscriber;
import cn.shyouchi.wuguanjia.android.ui.event.labor.LaborRequestEntity;
import cn.shyouchi.wuguanjia.android.ui.event.labor.PersonRank;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by Eenie on 2016/11/16 at 16:15
 * Email: 472279981@qq.com
 * Des:
 */

public class ArrangePresenter implements EventArrangeContract.Presenter {

    EventArrangeContract.View mView;
    Context mContext;
    EventApi mEventApi;

    public ArrangePresenter(Context context, EventArrangeContract.View view) {
        this.mContext = context;
        this.mView = view;
        mEventApi = new EventApi(context);
    }


    @Override
    public void attachView(@NonNull EventArrangeContract.View view) {

    }

    @Override
    public void detachView() {

    }


    @Override
    public void getArrangeList(final String projectId, final Date date) {

        mEventApi.getLaborList(projectId, new SimpleDateFormat("yyyy-MM-dd").format(date))
                .subscribeOn(Schedulers.io())
                .map(new Func1<LaborRequestEntity, List<LaborRequestEntity.ResultMessageBean>>() {
                    @Override
                    public List<LaborRequestEntity.ResultMessageBean> call(LaborRequestEntity laborRequestEntity) {
                        return laborRequestEntity.getResultMessage();
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<List<LaborRequestEntity.ResultMessageBean>>(mContext) {

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                        getPersonHours(projectId, new SimpleDateFormat("yyyy-MM-dd").format(date));
                    }

                    @Override
                    public void onNext(List<LaborRequestEntity.ResultMessageBean> resultMessageBeen) {

                        mView.onDataRefresh(resultMessageBeen);

                    }
                });


//        mEventApi.getArrange(projectId)
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new ProgressSubscriber<ArrangeEntity>(mContext) {
//                    @Override
//                    public void onNext(ArrangeEntity arrangeEntity) {
//                        mView.onDataRefresh();
//                    }
//                });
    }

    @Override
    public void delArrange(final String projectId, String id) {
        mEventApi.delArrange(id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<BaseRequestResult>(mContext) {
                    @Override
                    public void onNext(BaseRequestResult result) {
//                        getArrangeList(projectId);
                    }
                });
    }

    @Override
    public void addArrange(String projectid, String title, String startTime, String endTime) {

    }

    public void getPersonHours(final String projectId, final String date) {
        mEventApi.getPersonalHourslist(projectId, date)
                .subscribeOn(Schedulers.io())
                .map(new Func1<PersonRank, List<PersonRank.ResultMessageBean>>() {
                    @Override
                    public List<PersonRank.ResultMessageBean> call(PersonRank personRank) {
                        return personRank.getResultMessage();
                    }
                })
                .map(new Func1<List<PersonRank.ResultMessageBean>, List<PersonRankAdapter>>() {
                    @Override
                    public List<PersonRankAdapter> call(List<PersonRank.ResultMessageBean> resultMessageBeen) {
                        List<PersonRankAdapter> personRankAdapters = new ArrayList<>();
                        for (PersonRank.ResultMessageBean bean : resultMessageBeen) {
                            personRankAdapters.add(new PersonRankAdapter(bean));
                        }
                        return personRankAdapters;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<PersonRankAdapter>>() {
                    @Override
                    public void onCompleted() {
                        getschedulingList(projectId, date);
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(List<PersonRankAdapter> personRankAdapters) {
                        mView.onPersonRankRefresh(personRankAdapters);
                    }
                });
    }

    public void getschedulingList(String projectid, String date) {
        mEventApi.getschedulingList(projectid, date)
                .subscribeOn(Schedulers.io())
                .map(new Func1<RemoteArrangeEntity, List<RemoteArrangeEntity.ResultMessageBean>>() {
                    @Override
                    public List<RemoteArrangeEntity.ResultMessageBean> call(RemoteArrangeEntity remoteArrangeEntity) {
                        return remoteArrangeEntity.getResultMessage();
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<RemoteArrangeEntity.ResultMessageBean>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(List<RemoteArrangeEntity.ResultMessageBean> resultMessageBeen) {
                        mView.onRemoteArrangeChange(resultMessageBeen);
                    }
                });
    }


    public void schedulingAdd(String projectid) {
//        VirtualData virtualData = new VirtualData();
//        virtualData.setDate("2016-11-25");
//        List<VirtualData.Rank> ranks = new ArrayList<>();
//
//        List<VirtualData.User> users = new ArrayList<>();
//        VirtualData.User user1 = new VirtualData.User(3, "eenie");
//        VirtualData.User user2 = new VirtualData.User(6, "EENIE");
//        users.add(user1);
//        users.add(user2);
//
//        VirtualData.Rank rank1 = new VirtualData.Rank(53, "常日班");
//        rank1.setServiceid(53);
//        rank1.setUsers(users);
//
//
//        VirtualData.Rank rank2 = new VirtualData.Rank(55, "白班");
//        rank2.setServiceid(55);
//
//        List<VirtualData.User> users2 = new ArrayList<>();
//        VirtualData.User user21 = new VirtualData.User(3, "eenie");
//        VirtualData.User user22 = new VirtualData.User(6, "EENIE");
//        users2.add(user21);
//        users2.add(user22);
//        rank2.setUsers(users2);
//
//
//        VirtualData.Rank rank3 = new VirtualData.Rank(56, "夜班");
//        rank3.setServiceid(56);
//
//        List<VirtualData.User> users3 = new ArrayList<>();
//        VirtualData.User user31 = new VirtualData.User(3, "eenie");
//        VirtualData.User user32 = new VirtualData.User(6, "EENIE");
//        users3.add(user31);
//        users3.add(user32);
//
//        rank3.setUsers(users3);
//
//        ranks.add(rank1);
//        ranks.add(rank2);
//        ranks.add(rank3);
//        virtualData.setRanks(ranks);
//
//        LogUtil.json(virtualData);
//
//        mEventApi.schedulingAdd(projectid, virtualData)
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new ProgressSubscriber<BaseRequestResult>(mContext) {
//                    @Override
//                    public void onNext(BaseRequestResult result) {
//
//                    }
//                });
    }
}
